fix(spi): harden SQL execution and connection lifecycle#1924
Conversation
openai0229
left a comment
There was a problem hiding this comment.
Please split this PR into focused changes. It currently bundles nine unrelated fixes across connection-pool concurrency, transaction state, SQL generation, SQL Server schema handling, equality/hash behavior, and plugin lookup. These areas have different failure modes and need independent tests and rollback boundaries.
In particular, please add concurrency coverage for connection cleanup/queue behavior; preserve and restore the caller's original autoCommit state instead of forcing true; and test commit/rollback behavior around connection failures. The MySQL extent, SQL Server schema, equals/hashCode, and plugin lookup changes also need focused coverage.
The count SQL fix overlaps #1966. Please remove it here and let the smaller #1966 land independently.
buildExt() checked column.getExtent() for emptiness but returned column.getComment() — a copy-paste error from buildComment() above it.
…eName() The second condition duplicated the first, checking databaseName twice. When databaseName was blank but schemaName was not, the schema was skipped.
Empty catch blocks silently swallowed failures when setting catalog or schema, leaving the connection pointing at the wrong database with no indication of the problem.
- Cleanup thread now calls iterator.remove() after closing stale/dead connections, preventing unbounded queue growth - getConnection() returns ConnectInfo to queue when trySetInUse() fails, preventing connection leaks under concurrency
equals() compared {dataSourceId, gmtModified} but hashCode() used
{dataSourceId, consoleId, databaseName}, violating the Java contract.
Objects that are equal must have the same hashCode.
- ResultSet now in try-with-resources to prevent cursor leak - catch block calls connection.rollback() on failure - finally block restores connection.setAutoCommit(true) regardless of outcome Previously, exceptions left the connection in autoCommit=false with no rollback.
ResultSet from stmt.getResultSet() was not explicitly closed. Some JDBC drivers do not auto-close ResultSets when the parent Statement is closed, leading to cursor leaks under repeated use.
|
Updated the PR per review feedback:
The remaining focused changes are:
|
PLUGIN_MAP.get(dbType) returned null for unknown types, causing opaque NPE. Now throws IllegalArgumentException with the unsupported type name and list of registered types.
…0260725 # Conflicts: # chat2db-community-server/chat2db-community-plugins/chat2db-community-sqlserver/src/test/java/ai/chat2db/plugin/sqlserver/builder/SqlServerSqlBuilderTest.java
openai0229
left a comment
There was a problem hiding this comment.
Maintainer follow-up completed. Transaction ownership, rollback/abort cleanup, bounded queue handling, cleanup races, and stale lease invalidation are covered by focused tests. Final local reactor: 744 tests passed; diff check passed.
Related issue
N/A - this is a contributor maintenance PR without a separately tracked issue.
Summary
Hardens Community SPI SQL execution and pooled-connection lifecycle behavior, while retaining several focused correctness fixes from the original contribution.
The final change:
fetchAllTableRecords, rolls back failures, restores the originalautoCommitstate, and forcibly aborts a connection when rollback cannot be completed safely;ResultSetused bycount();ConnectInfo.hashCode()withequals()while keeping pool-generation state runtime-only;dbTypeand logs failures fromsetCatalog()/setSchema();Maintainer follow-up commits add transaction-ownership, rollback/abort, bounded-queue, cleanup-race, stale-lease, and generation-invalidation coverage.
Affected surfaces
Verification
rtk mvn -pl chat2db-community-plugins/chat2db-community-mysql,chat2db-community-plugins/chat2db-community-sqlserver -am test -Dmaven.test.skip=falsefromchat2db-community-server:BUILD SUCCESS; Tools 26, SPI 73, MySQL 607, SQL Server 38; 744 tests total, 0 failures, 0 errors, 0 skipped.rtk git diff --check origin/main...HEAD: passed.Risk and compatibility
Reviewer map
ConnectionPool, thenDefaultSQLExecutor.fetchAllTableRecords; focused tests areConnectionPoolTestandDefaultSQLExecutorTransactionTest.Contributor declaration
AI assistance: OpenAI Codex materially assisted the maintainer review, follow-up fixes, and test additions.